package ai.peanut.selfbase.modules.role.service.impl;

import ai.peanut.selfbase.common.constants.GlobalConstant;
import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.utils.UserUtils;
import ai.peanut.selfbase.modules.role.dao.RoleMapper;
import ai.peanut.selfbase.modules.role.entity.Role;
import ai.peanut.selfbase.modules.role.pojo.RoleParams;
import ai.peanut.selfbase.modules.role.service.IRoleService;
import ai.peanut.selfbase.modules.sys.entity.RoleMenu;
import ai.peanut.selfbase.modules.sys.service.IRoleMenuService;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lichong
 * @since 2019-02-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

@Autowired
private IRoleMenuService iRoleMenuService ;

    /**
     * 获取列表
     * @param
     * @param params
     * @return
     */
    @Override
    public Page<Role> selectAllPageByParams( RoleParams params ) {
        Page page = new Page(params.getCurrent()==null?1:params.getCurrent(),
                params.getSize()==null? GlobalConstant.WEB_PAGE_NUMBER:params.getSize());

        List<Role> list = baseMapper.selectAllByParams(page,params) ;
        page.setRecords(list) ;
        return  page ;
    }

    /**
     * 批量删除
     * @param params
     * @return
     */
    @Override
    public int deleteByIds( RoleParams params ) {
        return baseMapper.deleteByIds(params);
    }

    /**
     * 更新角色
     * @param params
     * @return
     */
    @Override
    public DataResult<Boolean> updateRole(RoleParams params){

        Date now = new Date() ;
        Role role = new Role() ;
        BeanUtils.copyProperties(params,role);
        Long userId = UserUtils.getCurrentUser().getId() ;
        role.setUpdateUserId(userId);
        role.setUpdateTime(now);
        role.setCreateTime(null);
        role.setCreateUserId(null);
        role.setDelFlag(null);
        this.updateById(role);
        iRoleMenuService.deleteByRoleId(role.getId());
        this.insertRoleMenu(role.getId() , params) ;
        return DataResult.success() ;
    }
    /**
     * 新增角色
     * @param params
     * @return
     */
    @Override
    public DataResult<Boolean> insertRole(RoleParams params){
        Date now = new Date() ;
        Role role = new Role() ;
        BeanUtils.copyProperties(params,role);
        Long userId = UserUtils.getCurrentUser().getId() ;
        role.setCreateUserId(userId);
        role.setUpdateUserId(userId);
        role.setCreateTime(now);
        role.setUpdateTime(now);
        role.setDelFlag(1);
        baseMapper.insertAllColumn(role);
//        int a = Integer.parseInt("23llpp4") ; //测试事务
        this.insertRoleMenu(role.getId() , params) ;

        return DataResult.success() ;
    }

    private void insertRoleMenu(Long roleId , RoleParams params){
        List<Long> menuIdList = params.getMenuIdList() ;
        if( menuIdList != null && menuIdList.size() > 0 ){
            for(Long id : menuIdList ){
                RoleMenu roleMenu = new RoleMenu() ;
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(id);
                iRoleMenuService.insertAllColumn(roleMenu);
            }

        }
    }


    /**
     * 根据用户id查找角色列表
     * @param userId
     * @return
     */
    @Override
    public List<Long> selectRoleIdListByUserId( Long userId){
        return baseMapper.selectRoleIdListByUserId( userId ) ;
    }

    /**
     * 获取列表 不分页
     * @param params
     * @return
     */
    @Override
    public List<Role> selectAllByParams(RoleParams params ) {
        return baseMapper.selectAllByParams(params) ;
    }
}
